﻿using System;
using System.Globalization;
using System.ServiceProcess;
using log4net;

namespace CML.Intercamber.Service
{
    public class IntercambersService : ServiceBase
    {
        #region gestion logs
        private static ILog logger;
        private static ILog Logger
        {
            get { return logger ?? (logger = LogManager.GetLogger(typeof (IntercambersService))); }
        }
        #endregion

        /// <summary>
        /// Point d'entrée principal de l'application.
        /// </summary>
        static void Main()
        {
            #region initialisation log4net & log start
            log4net.Config.XmlConfigurator.Configure();

            // on saute des lignes & start
            Logger.Info("");
            Logger.Info("--------------------------------------------------");
            Logger.Info("Demarrage du Service Intercambers");
            #endregion

            var servicesToRun = new ServiceBase[] 
            { 
                new IntercambersService() 
            };
            Run(servicesToRun);
        }

        private IntercambersService()
        {
            // Name the Windows Service
            ServiceName = "IntercambersService";
            CanPauseAndContinue = true;
            CanShutdown = true;
        }

        //private ServiceHost serviceHost;
        private TaskScheduler quartzScheduler;

        protected override void OnStart(string[] args)
        {
            try
            {
                //#region init WCF
                //serviceHost = new ServiceHost(typeof(TaskServiceService));
                //serviceHost.Open();
                //#endregion

                // test resources
                var culture = new CultureInfo("en");
                //string mailSubject = Resources.Intercamber.ResourceManager.GetString("Service_MessageNotificationSubject", culture);
                //mailSubject = mailSubject + " ";

                #region init quartz.net
                quartzScheduler = new TaskScheduler();
                quartzScheduler.Run();
                #endregion
            }
            catch (Exception e)
            {
                Logger.Fatal("Erreur lors du démarrage du service! " + e.Message);
                Logger.Fatal(e.StackTrace);
                throw;
            }
        }

        protected override void OnStop()
        {
            try
            {
                Logger.Info("Arret du service demandé, attente de l'arrêt des jobs");

                // stop wcf service
                //if (serviceHost != null)
                //{
                    //serviceHost.Close();
                //}

                // stop quartz jobs
                if (quartzScheduler != null)
                {
                    quartzScheduler.Stop();
                }

                Logger.Info("Le service est stoppé ----------------------------");
            }
            catch (Exception e)
            {
                Logger.Fatal("Erreur lors de l'arrêt du service! " + e.Message);
                Logger.Fatal(e.StackTrace);
                throw;
            }
        }

    }
}
